 package com.oanda.blackberry.restapi;
 
 import com.oanda.blackberry.net.ConnectionManager;
 import com.oanda.blackberry.net.RestException;
 import com.oanda.blackberry.net.UrlEncoder;
 import com.oanda.blackberry.util.log.Logger;
 import java.io.IOException;
 import java.io.PrintStream;
 
 public class Order extends Rest
 {
   public static long maxOrderId = 0L;
 
   public String list(String sessionToken, String accountId, String optMaxOrderId, String optMaxCount, String optSymbol, String optOrdersList)
     throws RestException
   {
     if (ConnectionManager.getUrl().equals("device")) {
       return "{\"open_orders\" : [{ \"id\" : 12345, \"type\": \"L\", \"symbol\" : \"EUR/USD\", \"units\" : 1000000, \"time\" : 1234567891, \"price\" : 1.5, \"stop_loss\" : 1.2, \"take_profit\" : 1.7, \"expiry\" : 1234567890, \"high_limit\" : 2.0, \"low_limit\" : 1.0, \"trailing_stop\" : 10},{ \"id\" : 12344, \"type\": \"L\", \"symbol\" : \"EUR/USD\", \"units\" : 100, \"time\" : 1234567890, \"price\" : 1.5, \"stop_loss\" : 1.2, \"take_profit\" : 1.7, \"expiry\" : 1234567890, \"high_limit\" : 2.0, \"low_limit\" : 1.0, \"trailing_stop\" : 10},{ \"id\" : 789, \"type\": \"L\", \"symbol\" : \"EUR/USD\", \"units\" : 100, \"time\" : 1234567890, \"price\" : 1.5, \"stop_loss\" : 1.2, \"take_profit\" : 1.7, \"expiry\" : 1234567890, \"high_limit\" : 2.0, \"low_limit\" : 1.0, \"trailing_stop\" : 10},{ \"id\" : 890, \"type\": \"L\", \"symbol\" : \"EUR/USD\", \"units\" : 100, \"time\" : 1234567890, \"price\" : 1.5, \"stop_loss\" : 1.2, \"take_profit\" : 1.7, \"expiry\" : 1234567890, \"high_limit\" : 2.0, \"low_limit\" : 1.0, \"trailing_stop\" : 10}]}";
     }
 
     String url = ConnectionManager.getUrl() + "v2/order/list.json";
     try {
       String parameters = "?session_token=" + UrlEncoder.encode(sessionToken, "") + (accountId != null ? "&account_id=" + UrlEncoder.encode(accountId, "") : "") + ((optMaxOrderId != null) && (optMaxOrderId.length() > 0) ? "&max_order_id=" + optMaxOrderId : "") + ((optMaxCount != null) && (optMaxCount.length() > 0) ? "&max_count" + optMaxCount : "") + ((optSymbol != null) && (optSymbol.length() > 0) ? "&symbol=" + UrlEncoder.encode(optSymbol, "") : "") + ((optOrdersList != null) && (optOrdersList.length() > 0) ? "&order_ids=" + UrlEncoder.encode(optOrdersList, "") : "");
 
       url = url + parameters;
     }
     catch (IOException e) {
     }
     url = url + ConnectionManager.getConnectionSuffix();
 
     this.response = proceedConnection(url);
 
     if (this.error) {
       throw new RestException(this.errorMessage + (this.response == null ? "" : this.response));
     }
     Logger.log("order.list response: " + this.response);
 
     return this.response;
   }
 
   public String delete(String sessionToken, String accountId, String orderId)
     throws RestException
   {
     if (ConnectionManager.getUrl().equals("device")) {
       return "{\"id\" : 12346,\"units\" : 150,\"price\" : 1.5,\"symbol\" : \"EUR/USD\",\"dir\" : \"L\"}";
     }
 
     String url = ConnectionManager.getUrl() + "v1/order/delete.json";
     String parameters = "";
     try {
       parameters = "?session_token=" + UrlEncoder.encode(sessionToken, "") + (accountId != null ? "&account_id=" + UrlEncoder.encode(accountId, "") : "") + (orderId != null ? "&order_id=" + UrlEncoder.encode(orderId, "") : "");
     }
     catch (IOException e)
     {
     }
 
     url = url + ConnectionManager.getConnectionSuffix();
 
     this.response = proceedConnection(url, parameters.substring(1));
 
     if (this.error) {
       throw new RestException(this.errorMessage + (this.response == null ? "" : this.response));
     }
     Logger.log("order.delete response: " + this.response);
     return this.response;
   }
 
   public String create(String sessionToken, String accountId, String symbol, String units, String price, String expirity, String optType, String optLowPrice, String optHighPrice, String optStopLoss, String optTakeProfit, String optTrailingStop)
     throws RestException
   {
     if (ConnectionManager.getUrl().equals("device")) {
       return "{\"id\" : 12345,\"units\" : 150,\"price\" : 1.5,\"symbol\" : \"EUR/USD\",\"dir\" : \"L\"}";
     }
 
     String url = ConnectionManager.getUrl() + "v1/order/create.json";
     String parameters = "";
     try {
       parameters = "?session_token=" + UrlEncoder.encode(sessionToken, "") + (accountId != null ? "&account_id=" + UrlEncoder.encode(accountId, "") : "") + (symbol != null ? "&symbol=" + UrlEncoder.encode(symbol, "") : "") + (units != null ? "&units=" + UrlEncoder.encode(units, "") : "") + (price != null ? "&price=" + UrlEncoder.encode(price, "") : "") + (expirity != null ? "&expiry=" + UrlEncoder.encode(expirity, "") : "") + ((optType != null) && (optType.length() > 0) ? "&type=" + UrlEncoder.encode(optType, "") : "") + ((optLowPrice != null) && (optLowPrice.length() > 0) ? "&low_price=" + UrlEncoder.encode(optLowPrice, "") : "") + ((optHighPrice != null) && (optHighPrice.length() > 0) ? "&high_price=" + UrlEncoder.encode(optHighPrice, "") : "") + ((optStopLoss != null) && (optStopLoss.length() > 0) ? "&stop_loss=" + UrlEncoder.encode(optStopLoss, "") : "") + ((optTakeProfit != null) && (optTakeProfit.length() > 0) ? "&take_profit=" + UrlEncoder.encode(optTakeProfit, "") : "") + ((optTrailingStop != null) && (optTrailingStop.length() > 0) ? "&trailing_stop=" + UrlEncoder.encode(optTrailingStop, "") : "");
     }
     catch (IOException e)
     {
     }
 
     url = url + ConnectionManager.getConnectionSuffix();
 
     this.response = proceedConnection(url, parameters.substring(1));
 
     if (this.error) {
       throw new RestException(this.errorMessage + (this.response == null ? "" : this.response));
     }
     Logger.log("order.create response: " + this.response);
     return this.response;
   }
 
   public String change(String sessionToken, String orderId, String accountId, String symbol, String type, String units, String price, String expiry, String optLowPrice, String optHighPrice, String optStopLoss, String optTakeProfit, String optTrailingStop)
     throws RestException
   {
     if (ConnectionManager.getUrl().equals("device")) {
       return "";
     }
     String url = ConnectionManager.getUrl() + "v1/order/change.json";
     String parameters = "";
     try {
       parameters = "?session_token=" + UrlEncoder.encode(sessionToken, "") + (orderId != null ? "&order_id=" + UrlEncoder.encode(orderId, "") : "") + (accountId != null ? "&account_id=" + UrlEncoder.encode(accountId, "") : "") + (symbol != null ? "&symbol=" + UrlEncoder.encode(symbol, "") : "") + (type != null ? "&type=" + UrlEncoder.encode(type, "") : "") + (units != null ? "&units=" + UrlEncoder.encode(units, "") : "") + (price != null ? "&price=" + UrlEncoder.encode(price, "") : "") + (expiry != null ? "&expiry=" + UrlEncoder.encode(expiry, "") : "") + ((optLowPrice != null) && (optLowPrice.length() > 0) ? "&low_price=" + UrlEncoder.encode(optLowPrice, "") : "") + ((optHighPrice != null) && (optHighPrice.length() > 0) ? "&high_price=" + UrlEncoder.encode(optHighPrice, "") : "") + ((optStopLoss != null) && (optStopLoss.length() > 0) ? "&stop_loss=" + UrlEncoder.encode(optStopLoss, "") : "") + ((optTakeProfit != null) && (optTakeProfit.length() > 0) ? "&take_profit=" + UrlEncoder.encode(optTakeProfit, "") : "") + ((optTrailingStop != null) && (optTrailingStop.length() > 0) ? "&trailing_stop=" + UrlEncoder.encode(optTrailingStop, "") : "");
     }
     catch (IOException e)
     {
     }
 
     url = url + ConnectionManager.getConnectionSuffix();
 
     System.out.println(parameters);
     this.response = proceedConnection(url, parameters.substring(1));
 
     if (this.error) {
       throw new RestException(this.errorMessage + (this.response == null ? "" : this.response));
     }
     Logger.log("order.change response: " + this.response);
     return this.response;
   }
 
   public String poll(String sessionToken, String accountId, String maxOrderId)
     throws RestException
   {
     if (ConnectionManager.getUrl().equals("device")) {
       return "{\"deleted\" : [10000, 10001],\"updated\" : [10002],\"created\" : [12347],\"max_order_id\" : 12349}";
     }
 
     String url = ConnectionManager.getUrl() + "v1/order/poll.json";
     try {
       String parameters = "?session_token=" + UrlEncoder.encode(sessionToken, "") + ((maxOrderId != null) && (maxOrderId.length() > 0) ? "&max_order_id=" + UrlEncoder.encode(maxOrderId, "") : "") + (accountId != null ? "&account_id=" + UrlEncoder.encode(accountId, "") : "");
 
       url = url + parameters;
     }
     catch (IOException e) {
     }
     url = url + ConnectionManager.getConnectionSuffix();
 
     this.response = proceedConnection(url);
 
     if (this.error) {
       throw new RestException(this.errorMessage + (this.response == null ? "" : this.response));
     }
     Logger.log("order.poll response: " + this.response);
     return this.response;
   }
 
   public static void setMaxOrderId(long maxOrderId_)
   {
     maxOrderId = maxOrderId_;
   }
 
   public static long getMaxOrderId() {
     return maxOrderId;
   }
}

